package com.bishe.demo.service;

import com.bishe.demo.mapper.CommodityMapper;
import com.bishe.demo.mapper.StoreMapper;
import com.bishe.demo.pojo.Commodity;
import com.bishe.demo.pojo.Store;
import com.bishe.demo.pojo.vo.Status;
import com.bishe.demo.utils.Judge;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.HashMap;
import java.util.List;

@Service
public class CommodityService {
    @Autowired
    CommodityMapper commodityMapper;

    @Autowired
    StoreMapper storeMapper;

    //添加商品
    public Status addCom( String commodityName,
                         Integer commodityNumber, String commodityCategory,
                         Integer commodityCost, Integer commodityPrice){
        Status status = Judge.judge(commodityName,commodityNumber,commodityCategory,commodityCost,commodityPrice);
        if (!status.getMsg().equals("成功")){
            System.out.println(status.getMsg());
            System.out.println("我执行了——————————————————————");
            return status;
        }
        HashMap<String,Object> map = new HashMap<>();
        map.put("commodity_name",commodityName);

        List<Store> storeList = storeMapper.selectByMap(map);
        System.out.println("commodityName"+commodityName);

        if (!storeList.get(0).getCommodityCategory().equals(commodityCategory)){
            status.setMsg("商品类型不匹配！");
            status.setCode(false);
            return status;
        }


        //如果数据中有该商品，则直接加数量
        if (commodityNumber>storeList.get(0).getCommodityNumber()){

            status.setMsg("库存不足！");
            status.setCode(false);
            return status;
        }

        List<Commodity> commodityList =  commodityMapper.selectByMap(map);


      if (commodityList.isEmpty()){
          System.out.println(commodityList.isEmpty());
           Commodity commodity = new Commodity();
           commodity.setCommodityCost(commodityCost);
           commodity.setCommodityName(commodityName);
           commodity.setCommodityPrice(commodityPrice);
           commodity.setCommodityCategory(commodityCategory);
           commodity.setCommodityNumber(commodityNumber);

           commodityMapper.insert(commodity);
          //store中减少数量,如果数量

          if (commodityNumber<storeList.get(0).getCommodityNumber()){
              Store store = new Store();
              store.setStoreId(storeList.get(0).getStoreId());
              store.setCommodityNumber(storeList.get(0).getCommodityNumber()-commodityNumber);
              storeMapper.updateById(store);
          }else{
              Store store = new Store();
             HashMap<String,Object> map1 = new HashMap<>();
             map1.put("commodity_name",commodityName);
              System.out.println("commodity中name"+commodityName);
              storeMapper.deleteByMap(map1);
          }


//          if ()
//
          status.setCode(true);
          status.setMsg("添加成功！");



      }else {
          Commodity commodity1 = new Commodity();
          System.out.println("service---"+commodityList.get(0).getCommodityName());
          commodity1.setCommodityId(commodityList.get(0).getCommodityId());
          commodity1.setCommodityNumber(commodityList.get(0).getCommodityNumber()+commodityNumber);
          System.out.println("service---"+commodityList.get(0).getCommodityId());
          System.out.println(commodity1.getCommodityId());
          commodityMapper.updateById(commodity1);
          if (commodityNumber<storeList.get(0).getCommodityNumber()){
           Store store = new Store();
           store.setStoreId(storeList.get(0).getStoreId());
           store.setCommodityNumber(storeList.get(0).getCommodityNumber()-commodityNumber);
           storeMapper.updateById(store);
          } else{
              HashMap<String,Object> map1 = new HashMap<>();
              map1.put("commodity_name",commodityName);
              System.out.println("commodity中name"+commodityName);
              storeMapper.deleteByMap(map1);
          }

          status.setCode(true);
          status.setMsg("添加成功！");
      }

        return  status;
    }

    public List<Commodity> selectAllCom(){

       List<Commodity> list = commodityMapper.selectList(null);

       return list;
    }
    //删除商品
    public Status deleteCom(Integer comId){
        Status status = new Status();
        if (comId==null){
            status.setMsg("没有可删除的商品！");
            status.setCode(false);
            return status;
        }
        HashMap<String,Object> map = new HashMap<>();
        map.put("commodity_id",comId);

        commodityMapper.deleteByMap(map);
        status.setMsg("删除成功！");

        status.setCode(true);
        return status;
    }
}
